home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / emulator / bsvc-1.000 / bsvc-1 / bsvc-1.0.4 / src / Framework / AddressSpace.cxx next >
C/C++ Source or Header  |  1995-07-26  |  6KB  |  237 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // $Id: AddressSpace.cxx,v 1.1 1994/02/18 19:47:06 bmott Exp $
  3. ///////////////////////////////////////////////////////////////////////////////
  4. // AddressSpace.cxx
  5. //
  6. //   This class maintains a list of devices and provides methods to peek and
  7. // poke into them.
  8. //
  9. //
  10. // BSVC "A Microprocessor Simulation Framework"
  11. // Copyright (c) 1993
  12. // By: Bradford W. Mott
  13. // October 31,1993 
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // $Log: AddressSpace.cxx,v $
  17. // Revision 1.1  1994/02/18  19:47:06  bmott
  18. // Initial revision
  19. //
  20. ///////////////////////////////////////////////////////////////////////////////
  21.  
  22. #include "String.h"
  23. #include "BasicCPU.hxx"
  24. #include "BasicDevice.hxx"
  25. #include "AddressSpace.hxx"
  26.  
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // AddressSpace Constructor
  29. ///////////////////////////////////////////////////////////////////////////////
  30. AddressSpace::AddressSpace(unsigned long m)
  31.     : maximum_address(m)
  32. {
  33.   head=tail=(void*)0;
  34. }
  35.  
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // The AddressSpace Destructor
  38. ///////////////////////////////////////////////////////////////////////////////
  39. AddressSpace::~AddressSpace()
  40. {
  41.   // Destroy all of the devices that are attached to the AddressSpace
  42.   while(DetachDevice(0)==1);
  43. }
  44.  
  45. ///////////////////////////////////////////////////////////////////////////////
  46. // Attach a device to the address space (1=OK,0=ERROR)
  47. ///////////////////////////////////////////////////////////////////////////////
  48. int AddressSpace::AttachDevice(BasicDevice* device)
  49. {
  50.   unsigned long maximum_in_bytes;
  51.  
  52.   maximum_in_bytes=((MaximumAddress()+1) * device->CPU()->Granularity())-1;
  53.  
  54.   if ((device->HighestAddress() >= device->LowestAddress()) &&
  55.       (device->HighestAddress() <= maximum_in_bytes) &&
  56.       (device->LowestAddress() <= maximum_in_bytes))
  57.   {
  58.     // Create the new node for the linked list
  59.     DeviceNode *p = new DeviceNode;
  60.     p->device=device;
  61.     p->next=(void*)0; 
  62.  
  63.     // Put the node at the end of the linked list
  64.     if (tail==(void*)0)
  65.     {
  66.       head=tail=p; 
  67.     }
  68.     else
  69.     {
  70.       tail->next=p;
  71.       tail=p; 
  72.     } 
  73.     return(1);
  74.   }
  75.   else
  76.   {
  77.     // Couldn't attach device so delete it!
  78.     delete device; 
  79.     return(0);
  80.   } 
  81. }
  82.  
  83. ///////////////////////////////////////////////////////////////////////////////
  84. // Detach a device from the address space and destory it (1=OK,0=ERROR)
  85. ///////////////////////////////////////////////////////////////////////////////
  86. int AddressSpace::DetachDevice(unsigned int index)
  87. {
  88.   DeviceNode *p,*q;
  89.  
  90.   // Make sure it's a valid index
  91.   if (index>=NumberOfAttachedDevices()) 
  92.     return(0);
  93.  
  94.   q=(void*)0;
  95.   p=head;
  96.   for(int t=0;(t<index) && (p!=(void*)0);++t)
  97.   {
  98.     q=p;
  99.     p=p->next;
  100.   }
  101.  
  102.   if (p!=(void*)0)
  103.   {
  104.     // Unlink the DeviceNode
  105.     if((p==head) && (p==tail))
  106.     {
  107.       head=tail=(void*)0;
  108.     }
  109.     else if (p==head)
  110.     {
  111.       head=p->next;
  112.     }
  113.     else if(p==tail)
  114.     {
  115.       q->next=(void*)0; 
  116.       tail=q;
  117.     }
  118.     else
  119.     {
  120.       q->next=p->next;
  121.     }
  122.  
  123.     // Free the device and the device node
  124.     delete p->device;
  125.     delete p;
  126.  
  127.     return(1);
  128.   }
  129.   else
  130.   {
  131.     return(0);
  132.   }
  133.  
  134. ///////////////////////////////////////////////////////////////////////////////
  135. // Reset all the attached devices
  136. ///////////////////////////////////////////////////////////////////////////////
  137. void AddressSpace::Reset()
  138. {
  139.   DeviceNode *p;
  140.  
  141.   for(p=head;p!=(void*)0;p=p->next)
  142.     p->device->Reset();
  143. }
  144.  
  145. ///////////////////////////////////////////////////////////////////////////////
  146. // Return the number of devices attached to the address space
  147. ///////////////////////////////////////////////////////////////////////////////
  148. int AddressSpace::NumberOfAttachedDevices()
  149. {
  150.   DeviceNode *p;
  151.   int t=0;
  152.  
  153.   for(p=head;p!=(void*)0;p=p->next)
  154.     ++t;
  155.  
  156.   return(t);
  157. }
  158.  
  159. ///////////////////////////////////////////////////////////////////////////////
  160. // Get device information for the indexed device  (return 1=OK,0=ERROR)
  161. ///////////////////////////////////////////////////////////////////////////////
  162. int AddressSpace::GetDeviceInformation(int index,
  163.         AddressSpaceDeviceInformation& info)
  164. {
  165.   DeviceNode *p;
  166.  
  167.   // Make sure it's a valid index
  168.   if(index>=NumberOfAttachedDevices()) 
  169.     return(0);
  170.  
  171.   p=head;
  172.   for(int t=0;(t<index) && (p!=(void*)0);++t)
  173.     p=p->next;
  174.  
  175.   if(p!=(void*)0)
  176.   {
  177.     info.name=p->device->Name();
  178.     info.initialization_arguments=p->device->InitializationArguments();
  179.     info.index=index;
  180.     return(1);
  181.   }
  182.   return(0);
  183. }
  184.  
  185. ///////////////////////////////////////////////////////////////////////////////
  186. // Peek a location in the address space 
  187. ///////////////////////////////////////////////////////////////////////////////
  188. int AddressSpace::Peek(unsigned long addr,unsigned char& c)
  189. {
  190.   DeviceNode* p;
  191.  
  192.   // Find the correct device
  193.   for(p=head;p!=(void*)0;p=p->next)
  194.     if (p->device->CheckMapped(addr))
  195.       break;
  196.  
  197.   // Did we find a device
  198.   if(p!=(void*)0)
  199.   {
  200.     // Get the byte
  201.     c=p->device->Peek(addr);
  202.     return(1);
  203.   }
  204.   else
  205.   {
  206.     // No device found BUS ERROR, dude!!!
  207.     return(0);
  208.   }
  209. }
  210.  
  211. ///////////////////////////////////////////////////////////////////////////////
  212. // Poke a location in the address space
  213. ///////////////////////////////////////////////////////////////////////////////
  214. int AddressSpace::Poke(unsigned long addr, unsigned char c)
  215. {
  216.   DeviceNode* p;
  217.  
  218.   // Find the correct device
  219.   for(p=head;p!=(void*)0;p=p->next)
  220.     if (p->device->CheckMapped(addr))
  221.       break;
  222.  
  223.   // Did we find a device
  224.   if(p!=(void*)0)
  225.   {
  226.     // Store the byte
  227.     p->device->Poke(addr,c);
  228.     return(1);
  229.   }
  230.   else
  231.   {
  232.     // No device found BUS ERROR, dude!!!
  233.     return(0);
  234.   }
  235. }
  236.